import java.io.Console;
import java.io.File;
import java.io.IOException;

import org.apache.commons.cli.BasicParser;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;

import com.adobe.ac.pmd.parser.exceptions.TokenException;
import com.moonshine.parser.finder.ClassInfoDB;
import com.moonshine.parser.finder.CompletionContext;
import com.moonshine.parser.finder.InfoDBFactory;
import com.moonshine.parser.finder.NodeFinder;

public class PonyComplete {

	public PonyComplete() {

	}

	public void console() {
		final Console console = System.console();
		String line = null;
		/**
		 * complete
		 */
		int argCount;
		String currentArgument;
		Options opt = getDefaultOptions();

		BasicParser parser = new BasicParser();
		while ((line = console.readLine()) != null) {

			try {
				CommandLine cl = parser.parse(opt, line.split(" "));

				if (cl.hasOption("method")) {

					String method = cl.getOptionValue("method");
					if (method.equals("completion")
							&& cl.hasOption("classPaths")
							&& cl.hasOption("file")) {
						invokePonyComplete(cl);

					}
				}
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}
	}

	private Options getDefaultOptions() {
		Options opt = new Options();

		opt.addOption("m", "method", true, "Method to run");
		opt.addOption("cp", "classPaths", true, "Class Path(s)");
		opt.addOption("f", "file", true, "File");
		opt.addOption("np", "nopretty",false, "Compress printing");
		return opt;
	}

	public void console(String[] args) {

		/**
		 * complete
		 */
		int argCount;
		String currentArgument;
		Options opt = getDefaultOptions();
		opt.addOption("l", "live", true, "Live Input");
		BasicParser parser = new BasicParser();

		try {

			CommandLine cl = parser.parse(opt, args);
			if (cl.hasOption("live")) {
				console();
			} else if (cl.hasOption("m")) {

				String method = cl.getOptionValue("m");
				if (method.equals("completion") && cl.hasOption("classPaths")
						&& cl.hasOption("file")) {
					invokePonyComplete(cl);

				}
			}
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	static final ClassInfoDB infoDB = InfoDBFactory.factory("realtime",
			"classinfo.db3");
	static final NodeFinder finder = new NodeFinder(infoDB);

	private void invokePonyComplete(CommandLine cl) {
		// TODO Auto-generated method stub
		String[] classPaths = cl.getOptionValue("classPaths").split(";"); // retrive
		// System.out.println(classPaths);
		// classPath
		String[] fileParts = cl.getOptionValue("file").split("@");
		Boolean shortClassNames = Boolean.parseBoolean(cl.getOptionValue(
				"short", "false"));

		// System.out.println(fileParts);
		CompletionContext context = invokePonyComplete(fileParts[0], Integer.valueOf(fileParts[1]), Integer
				.valueOf(fileParts[2]), classPaths);
		context.output("xml", System.out, !cl.hasOption("nopretty"), true);
	}

	public void invokeIndex(String baseFolder, String[] classPaths) {
		infoDB.setClassPaths(classPaths);
		long start = System.currentTimeMillis();
		// String file =
		// "C:\\DEV\\Personal Work\\mooneshine\\parser\\toplevel\\flash\\events\\NativeDragEvent.as";
		// infoDB.index(file);
		infoDB.index(new File(baseFolder));
		long end = System.currentTimeMillis() - start;
		System.out.println("End : " + end);
	}

	public CompletionContext invokePonyComplete(String file, int lineIndex, int charIndex,
			String[] classPaths) {
		finder.charIndex = charIndex;
		finder.lineIndex = lineIndex;
		CompletionContext context = new CompletionContext();
		try {
			infoDB.setClassPaths(classPaths);
			context = finder.find(file, classPaths);

		} catch (Exception e) {
			// TODO Auto-generated catch block

		}
		return context;
		
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		//System.out.println("Class Path :"+System.getProperties().getProperty("java.class.path"));
		PonyComplete p = new PonyComplete();
		if (args.length != 0) {
			// System.out.println(args);
			p.console(args);
		} else {

			String[] classPaths = new String[] {
					"C:\\DEV\\Personal Work\\mooneshine\\parser\\deploy\\intrinsic\\FP9",
					"C:\\DEV\\Personal Work\\mooneshine\\parser\\deploy\\intrinsic\\FP10",
					"C:\\DEV\\Personal Work\\mooneshine\\ide\\src" };
			// p.invokeIndex(classPaths[0], classPaths);
			// p.invokeIndex(classPaths[1], classPaths);

			String file = "C:\\DEV\\Personal Work\\mooneshine\\ide\\src\\com\\moonshineproject\\text\\SelectionManager.as";
			CompletionContext context = p.invokePonyComplete(file, 69,44, classPaths);
			context.output("xml", System.out, true, true);
			// 
			// long start = System.currentTimeMillis();
			//			
			// p.invokePonyComplete(file, 60, 15, classPaths, "xml");
			// long end = System.currentTimeMillis() - start;
			// System.out.println("End : " + end);
			// start = System.currentTimeMillis();
			// p.invokePonyComplete(file, 60, 15, classPaths, "xml");
			// end = System.currentTimeMillis() - start;
			// System.out.println("End : " + end);
			// String baseFolder =
			// "C:\\DEV\\Personal Work\\ConceptChat\\as3\\ConceptChat\\src";
			// p.invokeIndex(baseFolder, classPaths);
		}
		/*
		 * new PonyComplete(
		 * 
		 * , 27, 31);
		 */
	}

}
